package com.example.demo.juc.lock;

/**
 * @Author: xiatian
 * @CreateTime: 2021/9/16 11:41 上午
 * @Description:
 */
public class DeadLockSample extends Thread {

    private String first;
    private String second;

    public DeadLockSample(String name, String first, String second) {
        super(name);
        this.first = first;
        this.second = second;
    }

    @Override
    public void run() {

        synchronized (first) {
            System.out.println(this.getName() + "obtained:" + first);

            try {
                Thread.sleep(1000L);

                synchronized (second) {
                    System.out.println(this.getName() + "obtained:" + second);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        String lockA = "lockA";
        String lockB = "lockB";
        DeadLockSample t1 = new DeadLockSample("thread1 ", lockA, lockB);
        DeadLockSample t2 = new DeadLockSample("thread2 ", lockB, lockA);
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }

}
